package com.qg.cyapp.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.qg.cyapp.R;
import com.qg.cyapp.bean.FoodBean;
import com.qg.cyapp.component.MenuWaterFall;
import com.qg.cyapp.component.WaterFall;
import com.qg.cyapp.component.MenuWaterFall.ScrollToBottomListener;
import com.qg.cyapp.util.ShareUtil;

public class MedicinalDishesActivity extends HttpActivity implements
		ScrollToBottomListener {

	public static List<FoodBean> foods = new ArrayList<FoodBean>();
	private LinearLayout llFoods;
	private TextView tvSearch, tvSelect;
	private EditText etSearch;
	private MenuWaterFall wfFoods;
	private int startId = 0;
	private int number = 9;
	private boolean canLoad = false;

	
	@Override
	public void afterPost(String result, int requestCode,
			Map<String, String> request) {
		// TODO Auto-generated method stub

		bean = getGsonBean(result);
		if (bean.state == ShareUtil.SUCCESS) {
			List<FoodBean> newFoods = gson.fromJson(bean.json,
					new TypeToken<List<FoodBean>>() {
					}.getType());
			startId += newFoods.size();
			canLoad = true;
			foods.addAll(newFoods);
			wfFoods.notifyDataSetChanged();
		}
	}

	@Override
	public int getLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.activity_midicinal_dishes;
	}

	@Override
	public void setUI() {
		// TODO Auto-generated method stub
		llFoods = (LinearLayout) findViewById(R.id.llFoods);
		etSearch = (EditText) findViewById(R.id.et_menu_search);
		tvSearch = (TextView) mfindViewAndSetOnClick(R.id.tv_menu_searchBt);
		tvSelect = (TextView) findViewById(R.id.tv_title);

		wfFoods = new MenuWaterFall(this, foods ,ShareUtil.MedicinalDishesActivity);
		wfFoods.init(3);
		wfFoods.setScrollBottomListener(this);
		llFoods.addView(wfFoods);
		
		getNormalFood();
	}

	@Override
	public void OnClickListener(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.tv_menu_searchBt:
			Intent intent = new Intent(MedicinalDishesActivity.this,
					SearchShowActivity.class);
			String searchWord = etSearch.getText().toString();
			intent.putExtra(ShareUtil.SEARCH_KEY, searchWord);
			startActivity(intent);
			break;
		}
	}

	public void getNormalFood() {
		System.out.println("发送");
		params.clear();
		params.put("menudirno", ShareUtil.MEDICINAL_FOOD);
		params.put("startId", String.valueOf(startId));
		params.put("number", String.valueOf(number));
		params.put("action", "getFood");
		addPostRequest("GetFoodAction", params, 0);
		sendPostRequest();
	}
	
	@Override
	public void scrollToBottom() {
		// TODO Auto-generated method stub
		if (canLoad) {
			params.clear();
			params.put("cuisineno", ShareUtil.NORMAL_FOOD);
			params.put("startId", String.valueOf(startId));
			params.put("number", String.valueOf(number));
			params.put("action", "test");
			addPostRequest("GetFoodAction", params, 0);
			sendPostRequest();

			canLoad = false;
		}
	}

}
